+((w) => {
    w.utils.lyricFormat = (lyric, translatedLyric) => {
        if (translatedLyric) {
            let formattedMusicLyrics = [];
            //  需要有翻译版本
            lyric = format(lyric);
            translatedLyric = format(translatedLyric);
            for (let i = 0; i < translatedLyric.length; i++) {
                const element = translatedLyric[i];
                lyric.forEach(lyricElement => {
                    if (lyricElement.time == element.time) {
                        lyricTime = element.time;
                        lyric01 = {
                            originalLyric: lyricElement.lyric,
                            translatedLyric: element.lyric
                        }
                        formattedMusicLyrics.push({
                            time: lyricTime,
                            lyric: lyric01
                        })
                    }
                })
            }
            if (formattedMusicLyrics.length === 0) {
                return translatedLyric;
            } else {
                return formattedMusicLyrics;
            }
        } else {
            lyric = format(lyric)
            return lyric;
        }
    }
})(window)

function format(lyric) {
    let lyricRegex = /(\[[\d{2}:\d{2}\.\d{2,3}]+])([^\d{2}\d{2}\.\d{2,3}\[]+)/g;
    let lyricUnformattedArray = [];
    result = lyricRegex.exec(lyric);
    while (result) {
        if (result[2] !== "\n") {
            lyricUnformattedArray.push({
                time: result[1],
                lyric: result[2]
            });
        }
        result = lyricRegex.exec(lyric);
    }
    //  把数组时间转换成秒
    let lyricFormatted = [];
    for (let i = 0; i < lyricUnformattedArray.length; i++) {
        let element = lyricUnformattedArray[i].time;
        //  原时间格式为 [00.00.00] 进行格式化
        element = element.replace(/\[/g, "").replace(/]/g, "");
        element = element.split(".");
        let nanoSecond = element[1];
        let time = element[0];
        time = time.split(":");
        let minute = time[0];
        let second = time[1];
        let musicSecond =
            parseInt(minute * 60) +
            parseInt(second) +
            parseFloat(nanoSecond / 1000);
        lyricSentence = lyricUnformattedArray[i].lyric;
        lyricFormatted.push({
            time: musicSecond,
            lyric: lyricSentence
        })
    }
    function sortArray(n1, n2) {
        return n1.time - n2.time;
    }
    lyricFormatted.sort(sortArray)
    return lyricFormatted;
}